home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mgabra.zip / MGABRA.DOC < prev    next >
Text File  |  1989-07-27  |  61KB  |  1,557 lines

  1.  
  2.  
  3.  
  4.     ABRACADABRA! For Turbo C by Walt Howard Jr. 213-477-4151
  5.  
  6.  
  7.     SECTION 1 INTRODUCTION
  8.  
  9.     Welcome to Magic Software's Instant Resident Utility Maker
  10.     ABRACADABRA. Hopefully in no time at all we will be having you
  11.     create some amazing TSR programs with a simplicity BEFORE NOW
  12.     unheard of. We have also here at Magic coined a new term TCR
  13.     meaning Terminate but Continue Running which describes programs
  14.     which continue to run concurrently even while you are using the
  15.     pc to run a main program. These are just as easy to create with
  16.     ABRACADABRA.
  17.  
  18.     For me, writing TSRs used to be like first learning to program.
  19.     Without a library to facilitate me or even the know how to build
  20.     one I was somewhat in the dark. There wasn't even a book on the
  21.     subject. I was later to learn why. The secrets of this activity
  22.     were being guarded heavily by the few who knew them. I thought
  23.     this was completely at odds with the hacker ethic but what could
  24.     I do? Well after a few years of hobnobbing with these nuts,
  25.     geniuses and boneheads, each of whom had a piece of the puzzle I
  26.     was able to gather them all together and modularize them into
  27.     ABRACADABRA.
  28.  
  29.     For most of you, the fact that you were even interested in this
  30.     library guarantees you will have a fascinating time of it as
  31.     these secrets are revealed.
  32.  
  33.     If you are already familiar with these internals you can skip
  34.     right to the section describing the library primitives otherwise
  35.     read on.
  36.  
  37.     SECRETS OF TSR PROGRAMMING
  38.  
  39.     What makes a TSR a TSR? Certainly NOT just the fact that it
  40.     remains resident. Device drivers loaded in your CONFIG.SYS file
  41.     remain resident but they are not generally considered a TSR. A
  42.     TSR has several distinct qualities that define it.
  43.  
  44.     1. It remains in memory after initial loading from disk and can
  45.     be instantly invoked from memory.
  46.  
  47.     2. The invocation of a TSR must be able to occur WITHIN another
  48.     program. That's what gives them their utility value.
  49.  
  50.     3. The interrupted program can resume running after the TSR is
  51.     switched out.
  52.  
  53.     4. An additional quality we will add for a TCR (Terminate
  54.     Continue Running) is that it can continue to execute even after
  55.     it has been switched out.
  56.  
  57.  
  58.  
  59.  
  60.  
  61.                                     1
  62.  
  63.  
  64.  
  65.     ABRACADABRA! For Turbo C by Walt Howard Jr. 213-477-4151
  66.  
  67.  
  68.     When you are writing TSRs you are engaging in a limited degree of
  69.     multitasking. ABRACADABRA lets you take it to a full program swap
  70.     which means the ENTIRE program is switched, stack, data and all
  71.     INCLUDING screen and any disk access related memory items.
  72.  
  73.     It is no small feat to program but all this work has been
  74.     modularized for you in ABRACADABRA. If you have purchased the
  75.     source code then you can follow along in the next section,
  76.     otherwise you can skip right to the description of the
  77.     ABRACADABRA library functions and how to use them.
  78.  
  79.     What makes it so difficult? Why isn't it easy to write TSRs?
  80.  
  81.     Actually the program which swaps programs while complex, is not
  82.     the hardest part of this whole scenario. While complex, it is
  83.     quite abstract and orderly because it is a transaction that
  84.     occurs entirely between the CPU and memory. The 8086 has always
  85.     been somewhat friendly in those terms.
  86.  
  87.     The difficulties occur as you get further into the real world
  88.     away from the protected confines of the CPU and into the program
  89.     jungle called MS-DOS. DOS is essentially hostile toward attempts
  90.     at making it a multitasking program manager. So we have to do all
  91.     kinds of greasy kid stuff to make it comply with our wishes.
  92.     While the problem is complex, it is finite.
  93.  
  94.     The core of the problem, switching programs, is handled by simply
  95.     saving the swapped out programs complete register set and
  96.     replacing it with the swapped in programs registers. You can
  97.     examine this in more detail in the source listing of the macros
  98.     SAVPRC (save process) and RESPRC (restore process). When a
  99.     process is put to sleep it's registers are stored in a special
  100.     area loosely called the TASK CONTROL BLOCK (TCB). Other relevant
  101.     data is also stored here and we'll cover that next.
  102.  
  103.     Besides the registers, there is other baggage that each program
  104.     has attached that must also be switched. This consists of special
  105.     data areas DOS sets up for disk access, screen contents, video
  106.     modes, cursor position, and special interrupt addresses each
  107.     program uses for critical error handling and control-c break
  108.     checking.
  109.  
  110.     The unfriendly attitude DOS has regarding multitasking is very
  111.     deeply rooted in it's structure. A quick summary of what DOS is,
  112.     is simply a set of functions that a program can call to do
  113.     things. In this manner it is no different than a function library
  114.     you get with compiler type languages. It is designed however to
  115.     be called from anywhere. You don't have to know the actual
  116.     address of each function, you just load up your parameters and
  117.     execute a software interrupt 21h. This gives control of the
  118.     machine to DOS with the parameters you pass. It executes your
  119.     desire and returns control to your program when it is done.
  120.     However, DOS routines, like some languages, cannot handle
  121.  
  122.                                     2
  123.  
  124.  
  125.  
  126.     ABRACADABRA! For Turbo C by Walt Howard Jr. 213-477-4151
  127.  
  128.  
  129.     overlapping calls. You must finish one before another is
  130.     undertaken. This is because when you make a call, DOS switches
  131.     from your program's stack to it's own internal set. Whatever is
  132.     pushed here must be fully popped. If you try to call DOS before
  133.     it has finished a previous cycle everything goes fine but when
  134.     the previous cycle tries to complete it finds it's stack data has
  135.     been trashed. This is unfortunate because in a multitasking
  136.     environment you have programs which are all requesting DOS
  137.     assistance at roughly the same time.
  138.  
  139.     The way around this is to check if DOS is currently executing an
  140.     internal routine and have our programs wait until it is done
  141.     before requesting a subsequent service. A good solution would be
  142.     to latch onto the interrupt 21h and set a semaphore (flag)
  143.     whenever someone went through there and turn it off when they
  144.     came back out. That way we'd know when DOS was occupied.
  145.     Surprisingly DOS itself does just this and that flag is available
  146.     to our programs. There is an undocumented DOS feature which most
  147.     programmers call "The Dos Busy Flag" or "The Dos Critical Flag".
  148.     This flag exists at an address we can get by calling DOS function
  149.     34h. You won't find this in any Microsoft documentation. It is
  150.     simply listed as "Used Internally By Dos". So we get that address
  151.     and check that flag whenever we want to access DOS and if it's
  152.     busy we wait until it isn't.
  153.  
  154.     Because DOS is so finicky about when it can be called we cannot
  155.     simply invoke our TSR whenever someone asks. We must be polite
  156.     and wait until any DOS services are complete before we take over.
  157.     To do this we have to know whether DOS is busy or not.
  158.  
  159.     Now there is another set of routines called the BIOS (BASIC INPUT
  160.     OUTPUT SYSTEM) which is like DOS's DOS. DOS calls the BIOS when
  161.     it needs a nice prepackaged routine to access peripheral devices.
  162.     The BIOS has routines which are solely designed to organize
  163.     access to various devices on the PC such as disk drives, monitors
  164.     and clocks. The BIOS is actually stored in a chip on your pc and
  165.     each machine manufacturer supplies a BIOS (or should) when you
  166.     buy the machine. DOS is aptly named because it is a DISK
  167.     OPERATING SYSTEM. It's routines are very strong in handling disk
  168.     access but very weak in other areas. These weaknesses have made
  169.     programming the BIOS as common as using DOS. Something that
  170.     wasn't intended but has come about. Sort of like TSRs.
  171.  
  172.     The BIOS is particularly strong in vi